680. 验证回文串 II
为保证权益,题目请参考 680. 验证回文串 II(From LeetCode).
解决方案1
CPP
C++
/*
* LeetCode 680. 验证回文字符串 Ⅱ
* Author: Keven Ge
* Date: 2020-05-19
*/
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
bool validPalindrome(string s) {
int low = 0;
int high = s.length() - 1;
while (low <= high) {
if (s[low] == s[high]) {
low += 1;
high -= 1;
} else {
return help(s, low + 1, high) || help(s, low, high - 1);
}
}
return true;
}
bool help(string s, int a, int b) {
while (a <= b) {
if (s[a] == s[b]) {
a += 1;
b -= 1;
} else {
return false;
}
}
return true;
}
};
int main() {
Solution so;
cout << so.validPalindrome("cbbcc") << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49